//
//  SXTLoginViewController.m
//  8.18项目
//
//  Created by ma c on 16/8/21.
//  Copyright © 2016年 zhang. All rights reserved.
//

#import "SXTLoginViewController.h"
#import "SXTThirdLandingView.h"
#import "SXTLoginView.h"

@interface SXTLoginViewController ()

@property (strong, nonatomic)   SXTLoginView *loginView;
@property (strong, nonatomic)   SXTThirdLandingView *thirdView;              /** 第三方登录view */

@end

@implementation SXTLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = MainColor;
    self.edgesForExtendedLayout = 0;
    self.title = @"登录";
    [self addAutoLayout];
    
}

- (void)addAutoLayout{
    
    [self.view addSubview:self.loginView];
    [self.view addSubview:self.thirdView];
    
    __weak typeof (self) weakSelf = self;
    
    [_loginView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(weakSelf.view);
        make.height.equalTo(@240);
    }];
    
    
    [_thirdView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(weakSelf.loginView);
        make.top.equalTo(weakSelf.loginView.mas_bottom);
        make.height.equalTo(@85);
    }];
    
}

- (SXTLoginView *)loginView{
    if (!_loginView) {
        _loginView = [[SXTLoginView alloc]init];
        
        __weak typeof (self) weakSelf = self;
        _loginView.goLoginBlock = ^(NSDictionary *userDic){
            [weakSelf loginMethod:userDic];
        };
    }
    return _loginView;
}

- (SXTThirdLandingView *)thirdView{
    if (!_thirdView) {
        _thirdView = [[SXTThirdLandingView alloc]init];
        __weak typeof(self) weakSelf = self;
        _thirdView.qqBlock = ^(){
        
            [weakSelf qqLandingMethod];
        };
    }
    return _thirdView;
}

- (void)loginMethod:(NSDictionary *)userDic{
    [self POSTRequestURL:@"appMember/appLogin.do" param:userDic isSuccess:^(id responseObject) {
        if ([responseObject[@"ErrorMessage"]isEqualToString:@"登录成功"]) {
            [self showTostInView:@"登录成功"];
            [[NSUserDefaults standardUserDefaults] setObject:responseObject forKey:@"ISLOGIN"];
            [self performSelector:@selector(popMyViewController) withObject:nil afterDelay:1.0];
        }else if ([responseObject[@"ErrorMessage"] isEqualToString:@"密码错误"]){
        
            [self showTostInView:@"密码错误"];
        }else if ([responseObject[@"ErrorMessage"] isEqualToString:@"用户不存在"]){
        
            [self showTostInView:@"用户不存在"];
        }
        else{
            [self showTostInView:@"登录失败"];
        }
    } isError:^(NSError *error) {
        NSLog(@"error:%@",error);
    }];

}
//判断是否已经登录
-(void)popMyViewController{

    [self.navigationController popToRootViewControllerAnimated:YES];
    
}
//qq登录
- (void)qqLandingMethod{
    NSLog(@"qq登陆成功");
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
